<?php
namespace App\Services;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Storage;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Redis;
use Log,Exception,Config;
use Symfony\Component\Cache\Adapter\RedisAdapter;

class WechatcheckService
{
    public static function openPlatform()
    {
        $config = Config::get("wechats.open_platform.checkmini");
        $openPlatform = Factory::openPlatform($config);
        $predis = app('redis')->connection('cache')->client();
        $cache = new RedisAdapter($predis);
        $openPlatform->rebind('cache', $cache);
        return $openPlatform;
    }

    // 获取开发component_access_token
    public static function getComponentAccessToken()
    {
        $openPlatform = self::openPlatform();
        $accessToken = $openPlatform->access_token;
        $token = $accessToken->getToken();
        $component_access_token = $token["component_access_token"];
        return $component_access_token;
    }

    // 获取代小程序access_token
    public static function getMiniProgramAccessToken($appId,$refreshToken)
    {
        $openPlatform = self::openPlatform();
        $miniProgram = $openPlatform->miniProgram($appId,$refreshToken);
        $accessToken = $miniProgram->access_token;
        $token = $accessToken->getToken();
        $authorizer_access_token = $token["authorizer_access_token"];
        return $authorizer_access_token;
    }

    // 刷新access_token
    public static function getMiniProgramAccessTokenForce($appId,$refreshToken)
    {
        $openPlatform = self::openPlatform();
        $miniProgram = $openPlatform->miniProgram($appId,$refreshToken);
        $accessToken = $miniProgram->access_token;
        $token = $accessToken->getToken(true);
        $authorizer_access_token = $token["authorizer_access_token"];
        return $authorizer_access_token;
    }

    // 获取小程序基本信息
    public static function getAccountBasicInfo($authorizer_appid,$authorizer_refresh_token)
    {
        $authorizer_access_token = self::getMiniProgramAccessToken($authorizer_appid,$authorizer_refresh_token);
        $client = new Client;
        $response = $client->request('GET', 'https://api.weixin.qq.com/cgi-bin/account/getaccountbasicinfo?access_token='.$authorizer_access_token);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        return $jsonbody;
    }

    // 获取预授权码
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/pre_auth_code.html
    public static function apiCreatePreauthcode()
    {
        $config = Config::get("wechats.open_platform.checkmini");
        $component_appid = $config["app_id"];
        $component_access_token = self::getComponentAccessToken();
        $client = new Client;
        $params = ["component_appid"=>$component_appid];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        return json_decode($body,true);
    }

    // 使用授权码获取授权信息
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/authorization_info.html
    public static function apiQueryAuth($authorization_code)
    {
        $config = Config::get("wechats.open_platform.checkmini");
        $component_appid = $config["app_id"];
        $component_access_token = self::getComponentAccessToken();
        $client = new Client;
        $params = ["component_appid"=>$component_appid,"authorization_code"=>$authorization_code];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        return json_decode($body,true);
    }

    // 获取授权方的帐号基本信息
    // https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/api_get_authorizer_info.html
    public static function apiGetAuthorizerInfo($authorizer_appid)
    {
        $config = Config::get("wechats.open_platform.checkmini");
        $component_appid = $config["app_id"];
        $component_access_token = self::getComponentAccessToken();
        $client = new Client;
        $params = ["component_appid"=>$component_appid,"authorizer_appid"=>$authorizer_appid];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token='.$component_access_token, [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]]);
        $body = $response->getBody();
        return json_decode($body,true);
    }
}